#!/bin/sh

PRONAME=$PRODUCT_NAME
[ -n "$PRODUCT_SUBNAME" ] && PRONAME="$PRODUCT_NAME"_"$PRODUCT_SUBNAME"
#PRODUCT_NAME=StorageHouseManage
REPO_PATH=http://172.20.220.100/svn/rCortex210/trunk/code/rootfs-a8
REVISION=$(LANG=C svn info $REPO_PATH | awk -F: '{ if ($1 == "Last Changed Rev") print $2; }' | sed 's/^[ \t]*//g')
SUBREPO_PATH=$(LANG=C svn info | awk '{ if ($1=="URL:") print $2; }' | sed 's/^[ \t]*//g')
SUBVERSION=$(LANG=C svn info $SUBREPO_PATH | awk -F: '{ if ($1 == "Last Changed Rev") print $2; }' | sed 's/^[ \t]*//g')

[ -z "$REVISION" ] && exit 1
SYS_TARGET_PATH=$PRONAME-system-r$REVISION-$SUBVERSION
CACHE_TARGET_PATH=$PRONAME-cache-r$REVISION-$SUBVERSION
USERDATA_TARGET_PATH=$PRONAME-userdata-r$REVISION-$SUBVERSION
SYS_YAFFS_PATH=$SYS_TARGET_PATH.yaffs.bin
CACHE_YAFFS_PATH=$CACHE_TARGET_PATH.yaffs.bin
USERDATA_YAFFS_PATH=$USERDATA_TARGET_PATH.yaffs.bin
[ -e $SYS_TARGET_PATH ] && rm -rf $SYS_TARGET_PATH
[ -e $CACHE_TARGET_PATH ] && rm -rf $CACHE_TARGET_PATH
[ -e $USERDATA_TARGET_PATH ] && rm -rf $USERDATA_TARGET_PATH
[ -e $SYS_YAFFS_PATH ] && rm -rf $SYS_YAFFS_PATH
[ -e $CACHE_YAFFS_PATH ] && rm -rf $CACHE_YAFFS_PATH
[ -e $USERDATA_YAFFS_PATH ] && rm -rf $USERDATA_YAFFS_PATH

# checkout rootfs
echo "rootfs --> $SYS_TARGET_PATH"
svn export $REPO_PATH $SYS_TARGET_PATH >/dev/null
mknod $SYS_TARGET_PATH/dev/console c 5 1

# delete QtEmbedded-4.8.5-arm
rm -rf $SYS_TARGET_PATH/usr/local/Trolltech/QtEmbedded-4.8.5-arm
rm -rf $SYS_TARGET_PATH/usr/local/Trolltech/QtEmbedded-4.7.0-arm/lib/fonts/*

# install wsnService
cp -a rootfs/* $SYS_TARGET_PATH/
# generate version.info file
echo "$PRONAME-r$REVISION.$SUBVERSION" > $SYS_TARGET_PATH/Application/$PRODUCT_NAME/version.info

chown root:root -R $SYS_TARGET_PATH

[ "$1" = "rootfs" ] && exit 0

# split the rootfs into three parts: system(rootfs) - cache(Application) - userdata(root)
mv $SYS_TARGET_PATH/Application $CACHE_TARGET_PATH
mkdir $SYS_TARGET_PATH/Application
mv $SYS_TARGET_PATH/root $USERDATA_TARGET_PATH
mkdir $SYS_TARGET_PATH/root

# make yaffs image
./mkyaffs2image $SYS_TARGET_PATH $SYS_YAFFS_PATH
chmod 0777 $SYS_YAFFS_PATH
./mkyaffs2image $CACHE_TARGET_PATH $CACHE_YAFFS_PATH
chmod 0777 $CACHE_YAFFS_PATH
./mkyaffs2image $USERDATA_TARGET_PATH $USERDATA_YAFFS_PATH
chmod 0777 $USERDATA_YAFFS_PATH

rm -rf $SYS_TARGET_PATH
rm -rf $CACHE_TARGET_PATH
rm -rf $USERDATA_TARGET_PATH

echo "Done!"
echo
echo "[$PRONAME]"
echo "bootloader=u-boot.bin"
echo "ramdisk="
echo "kernel=zImage.linux.board"
echo "system=$SYS_YAFFS_PATH"
echo "cache=$CACHE_YAFFS_PATH"
echo "userdata=$USERDATA_YAFFS_PATH"
echo "android=0"
echo
